#include "os.h"

#define DELAY 4000
struct userdata {
    int counter;
    char *str;
};

/* Jack and Rose must be global */
struct userdata person0 = {0, "task0"};
struct userdata person1 = {0, "task1"};


void user_task0(void* param)
{
    struct userdata *data = (struct userdata *)param;
    uart_puts("Task 0: Created!\n");
    for(int i=0;i<4;i++) {
        printf("Task 0: Running... Counter: %d, Name: %s\n", data->counter, data->str);
        task_delay(DELAY);
        data->counter++;
    }
	
    printf("Task 0: Finished!\n");
    task_exit();

}

void user_task1(void* param)
{
    struct userdata *data = (struct userdata *)param;
    uart_puts("Task 1: Created!\n");
    while (1) {
        printf("Task 1: Running... Counter: %d, Name: %s\n", data->counter, data->str);
        task_delay(DELAY);
        data->counter++;
    }
}

/* NOTICE: DON'T LOOP INFINITELY IN main() */
void os_main(void)
{
    // 创建任务并传递参数
    // 设置优先级和时间片
    task_create(user_task0, &person0, 2, 2); // 优先级为 2，时间片为 5 ticks
    task_create(user_task1, &person1, 1, 4); // 优先级为 1，时间片为 10 ticks

    // 主任务退出，交由调度器管理
    task_exit();
}
